<?php
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/../../common/config/params-local.php'),
    require(__DIR__ . '/params.php'),
    require(__DIR__ . '/statuscode.php'),
    require(__DIR__ . '/params-local.php')
);

$rules = array_merge(
    require(__DIR__ . '/rules/ua.php')
);

return [
    'id' => 'api',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'api\controllers',
    'bootstrap' => ['log'],
    'modules' => [
        'ua' => ['class' => 'api\modules\ua\Module'],
    ],
    'components' => [
        'request' => [
            'csrfParam' => 'csrfApi',
            'parsers' => ['application/json' => 'yii\web\JsonParser']
        ],

        'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;
                if($response->data !== null){
                    $code = Yii::$app->response->statusCode;

                    //代码信息
                    if(isset($response->data['code'])){
                        $response->data['message'] = Yii::$app->params['codes'][$response->data['code']];
                    }

                    $response->data = [
                        'success'   => $response->isSuccessful,
                        'status'    => $code,
                        'text'      => Yii::$app->params['codes'][$code],
                        'data'      => $response->data,
                    ];

                }
            },
        ],

        'user' => [
            'identityClass'     => 'api\models\User',
            'enableAutoLogin'   => true,
            'enableSession'     => false,
        ],

        'session' => ['name' => 'apiSession'],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => ['errorAction' => 'site/error'],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' =>true,
            'rules' => $rules,
        ],
    ],
    'params' => $params,
];
